package com.ba.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ba.objects.Urun;

public class UrunIslemleri {

	public List<Urun> getDBUrunList() {
		ConnectionUtil connectionUtil = new ConnectionUtil(DBConfig.DB_DRIVER,
				DBConfig.DB_CONNECTION_URL, DBConfig.DB_USERNAME,
				DBConfig.DB_PASSWORD);

		List<Urun> urunList = new ArrayList<Urun>();
		Statement statement = connectionUtil.getStatement();
		int id = 1;
		String sql = "SELECT * FROM ETIC_URUN WHERE id =" + id;
		// String sql = "select * from etic_URun";
		try {
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				Urun urun = new Urun();
				urun.setId(rs.getInt("ID"));
				urun.setKategoriId(rs.getInt("KATEGORI_ID"));
				urun.setMarkaId(rs.getInt("MARKA_ID"));
				urun.setAd(rs.getString("AD"));
				urun.setFiyat(rs.getBigDecimal("FIYAT"));
				urun.setResim(rs.getString("RESIM"));
				urun.setAciklama(rs.getString("ACIKLAMA"));
				urun.setStok(rs.getInt("STOK"));
				urunList.add(urun);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionUtil.closeConnection();
		}
		return urunList;
	}

	public List<Urun> getDBUrunListUseBindVariable() {
		ConnectionUtil connectionUtil = new ConnectionUtil(DBConfig.DB_DRIVER,
				DBConfig.DB_CONNECTION_URL, DBConfig.DB_USERNAME,
				DBConfig.DB_PASSWORD);

		List<Urun> urunList = new ArrayList<Urun>();
		Connection connection = connectionUtil.getConnection();

		String sql = "SELECT * FROM ETIC_URUN WHERE id < ? and id > ?";

		// String sql = "select * from etic_URun";
		try {
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setInt(1, 5);
			preparedStatement.setInt(2, 2);
			// ResultSet rs = statement.executeQuery(sql);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				Urun urun = new Urun();
				urun.setId(rs.getInt("ID"));
				urun.setKategoriId(rs.getInt("KATEGORI_ID"));
				urun.setMarkaId(rs.getInt("MARKA_ID"));
				urun.setAd(rs.getString("AD"));
				urun.setFiyat(rs.getBigDecimal("FIYAT"));
				urun.setResim(rs.getString("RESIM"));
				urun.setAciklama(rs.getString("ACIKLAMA"));
				urun.setStok(rs.getInt("STOK"));
				urunList.add(urun);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionUtil.closeConnection();
		}
		return urunList;
	}
}
